using System.Collections.Generic;
using UnityEngine;

public class ShopControlLayer : MonoBehaviour
{
	public StoreItemsConfig.StoreItem.ItemType type;

	public ShopItemButton buttonPrefab;

	public UIGrid grid;

	public UIScrollView view;

	private List<ShopItemButton> shopItems = new List<ShopItemButton>();

	private List<StoreItemsConfig.StoreItem> _activeItemList;

	private bool isAligned;

	private List<StoreItemsConfig.StoreItem> activeItemList
	{
		get
		{
			if (_activeItemList == null)
			{
				_activeItemList = StoreItemsConfig.instance.GetItemsForType(type, false);
			}
			return _activeItemList;
		}
	}

	private void OnEnable()
	{
		GeneratePage();
	}

	private void GeneratePage()
	{
		for (int i = 0; i < activeItemList.Count; i++)
		{
			if (i >= shopItems.Count)
			{
				ShopItemButton component = NGUITools.AddChild(grid.gameObject, buttonPrefab.gameObject).GetComponent<ShopItemButton>();
				component.gameObject.name = GGFormat.IndexName(i);
				shopItems.Add(component);
			}
			InitialiseButton(shopItems[i], activeItemList[i]);
		}
		for (int j = activeItemList.Count; j < shopItems.Count; j++)
		{
			InitialiseButton(shopItems[j], null);
		}
		grid.Reposition();
		isAligned = false;
	}

	private void InitialiseButton(ShopItemButton button, StoreItemsConfig.StoreItem item)
	{
		button.gameObject.SetActive(item != null);
		if (item != null)
		{
			button.Init(item);
		}
	}

	private void Update()
	{
		if (!isAligned && shopItems.Count > 0)
		{
			UITools.AlignToLeftOnScroll(shopItems[0].gameObject.GetComponent<UIWidget>(), view, 40f);
			isAligned = true;
		}
	}
}
